Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further asyncify the codebase #890

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jprendes
Copy link
Collaborator

@jprendes jprendes commented Feb 28, 2025

TL.DR.: This PR sprinkles async and await throughout the codebase.

This PR further introduces async to the codebase without the leap of switching to async rust-extensions.

The reason not to switch to async rust-extensions yet is that it doesn't work on windows (I.(i.e., it doesn't build on Windows, so our CI fails).
There are a few options here for the future:

  • not build on Windows in CI
  • add windows support to async ttrpc-rust and rust-extensions.
  • create an adapter that sits in between our codebase and rust-extensions that allows us to build with either sync or async rust-extensions (I have a working prototype of this already)

This PR uses our "ambient" tokio runtime to run our async codebase inside of the sync methos that rust-extensions expects.

This PR doesn't convert our testing framework to async either. This is to reduce the amount of changes.

@jprendes jprendes force-pushed the mostly-async branch 7 times, most recently from 1b01571 to 36a7cf5 Compare February 28, 2025 23:35
@jprendes jprendes changed the title make methods in Local async Further asyncify the codebase Mar 1, 2025
@jprendes
Copy link
Collaborator Author

jprendes commented Mar 2, 2025

There's an odd CI failure here
https://github.com/containerd/runwasi/actions/runs/13598352245/job/38019970662#step:10:950
That I haven't been able to reproduce.
I didn't look too much into it, but I think it's an already existing race condition in our codebase. I suspect it's related to how we wait for the process to exit in different parts of the codebase. One of the things after that happens is change the state from running to stopped, but we also wait and so things in other places.
I will investigate further, but I don't think it needs to be addressed in this PR.

@jprendes jprendes marked this pull request as ready for review March 2, 2025 18:39

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request introduces async/await throughout the codebase while keeping a synchronous interface where needed by using the ambient Tokio runtime. Key changes include refactoring existing synchronous APIs to async versions (with adjustments in instance creation, start, kill, delete, and wait methods), updating tests to run under Tokio, and switching dependencies and types (e.g. using tokio’s RwLock and replacing ExitSignal with WaitableCell).

Reviewed Changes

File Description
typos.toml Added new rules to ignore false‐positive typos.
crates/containerd-shim-wasm/src/testing.rs Converted instance operations to use async (with .block_on()) in sync contexts.
crates/containerd-shim-wasm/src/sandbox/shim/local.rs Changed several methods from blocking to async, updating lock handling and usage of get_instance, task_start, task_kill, etc.
Cargo.toml Updated Tokio features and added new dependencies (trait-variant, tokio-async-drop).
crates/containerd-shim-wasm/src/sandbox/cli.rs Adjusted CLI to run prehook code within an async block and updated exit handling.
Various tests Updated tests to use Tokio’s async runtime and channels, including replacing std::sync channels with Tokio channels.
Instance APIs (instance_data.rs, instance.rs, etc.) Updated trait definitions and implementations to be fully async, replacing OnceLock with OnceCell and adjusting sync wrappers.

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

crates/containerd-shim-wasm/src/sandbox/shim/local.rs:124

  • Review the use of .block_on() in synchronous trait implementations that rely on this async function, as it may risk deadlocking if the ambient Tokio runtime is not properly isolated. Consider propagating async context or creating dedicated sync wrappers to reduce the risk.
pub(super) async fn get_instance(&self, id: &str) -> Result<Arc<InstanceData<T>>> {

crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs:40

  • Ensure that all async calls (like instance start) are properly awaited and that the ambient runtime is initialized to avoid unintentional blocking; verify that converting these methods to async does not introduce execution order issues in production.
let res = self.instance.start().await;
jprendes added 2 commits March 3, 2025 07:57
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
@jprendes
Copy link
Collaborator Author

jprendes commented Mar 3, 2025

rebased

Mossaka
Mossaka previously approved these changes Mar 4, 2025
Copy link
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

… c8d leases

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Copy link
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Could you please update the changelog?

// It will stop executing when dropped.
// We need to keep this future's lifetime tied to this
// method's lifetime.
// This means we shouldn't tokio::spawn it, but ruther
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This means we shouldn't tokio::spawn it, but ruther
// This means we shouldn't tokio::spawn it, but rather

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants